﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using log4net;
using CashFlow.Db.Entity;
using SysAdmin._00_Def;
using SysAdmin._03_Service._00_Db;
using SysAdmin._04_Form._00_Common;

namespace SysAdmin._04_Form._01_Shop
{
    public partial class SelectShopUserForm : Form
    {
        private static ILog _logger = LogManager.GetLogger(typeof(SelectShopUserForm));
        private Timer _timer = new Timer();
        private IList<UserInfoEntity> AllUserList { get; set; }
        public IList<UserInfoEntity> SelectedUserInfoList { get; private set; } = new List<UserInfoEntity>();
        public int ParentShopManagerUserId { get; set; }

        public SelectShopUserForm()
        {
            InitializeComponent();
        }

        private void AllUserForm_Load(object sender, EventArgs e)
        {
            treeView1.Height = ClientSize.Height - (Padding.Top + Padding.Bottom) - panel1.Height - 10;
            _timer.Interval = 100;
            _timer.Tick += _timer_Tick;
        }

        private void AllUserForm_Shown(object sender, EventArgs e)
        {
            _timer.Start();
        }

        private void _timer_Tick(object sender, EventArgs e)
        {
            try
            {
                _timer.Stop();
                AllUserList = UserService.QueryAllChildUsersByUserId(ParentShopManagerUserId);
                InitTreeView();
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                UICommonTool.ShowAlertDialog(ex.Message);
            }
        }

        private void InitTreeView()
        {
            var ui = AllUserList.Where(x => x.Id == ParentShopManagerUserId).First();
            var rootNode = new TreeNode(ui.Name);
            rootNode.Tag = ui;
            Queue<string> queue = new Queue<string>();
            queue.Enqueue(ui.InvitationCode);
            UserInfoEntity parentUserInfo = ui;
            List<TreeNode> parentNodes = new List<TreeNode>();
            parentNodes.Add(rootNode);
            var tmpParentNodes = new List<TreeNode>();
            int index = 0;
            while (queue.Count > 0)
            {
                string invitationCode = queue.Dequeue();
                var ls = AllUserList.Where(x => x.ReferenceInvitationCode == invitationCode).ToList();

                foreach (var item in ls)
                {
                    var node = new TreeNode(item.Name);
                    node.Tag = item;
                    node.ToolTipText = string.Format("{0} - {1}", item.Name, item.Phone);
                    parentNodes[index].Nodes.Add(node);
                    queue.Enqueue(item.InvitationCode);
                    tmpParentNodes.Add(node);
                }

                index++;
                if (index == parentNodes.Count)
                {
                    parentNodes.Clear();
                    parentNodes.AddRange(tmpParentNodes.ToArray());
                    tmpParentNodes.Clear();
                    index = 0;
                }
            }

            treeView1.BeginUpdate();
            treeView1.Nodes.Add(rootNode);
            treeView1.EndUpdate();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Checked)
            {
                e.Node.ExpandAll();
            }

            SetChildNodesChecked(e.Node, e.Node.Checked);
        }

        private void SetChildNodesChecked(TreeNode node, bool bChecked)
        {
            var ls = new List<TreeNode>();
            foreach (TreeNode item in node.Nodes)
            {
                item.Checked = bChecked;
                ls.Add(item);
            }

            foreach (TreeNode item in ls)
            {
                SetChildNodesChecked(item, bChecked);
            }
        }

        private void SelectShopUserInfo(TreeNode node)
        {
            var ls = new List<TreeNode>();
            foreach (TreeNode item in node.Nodes)
            {
                ls.Add(item);
                if (item.Checked)
                {
                    var ui = item.Tag as UserInfoEntity;
                    SelectedUserInfoList.Add(ui);
                }
            }

            foreach (TreeNode item in ls)
            {
                SelectShopUserInfo(item);
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            SelectedUserInfoList.Clear();
            SelectShopUserInfo(treeView1.Nodes[0]);
            if (SelectedUserInfoList.Count > 0)
            {
                DialogResult = DialogResult.OK;
                Close();
            }
            else
            {
                UICommonTool.ShowAlertDialog("没有选择任何会员！");
            }
        }
    }
}